package com.scuec.groupfive.service.impl;

import com.scuec.groupfive.entity.WrongQuestion;
import com.scuec.groupfive.mapper.WrongQuestionMapper;
import com.scuec.groupfive.service.WrongQuestionService;
import com.scuec.groupfive.vo.WrongQuestionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WrongQuestionServiceImpl implements WrongQuestionService {
    @Autowired
    private WrongQuestionMapper wrongQuestionMapper;

    @Override
    public Map<String, Object> getByWrongCountDesc(Long userId, Integer page, Integer size, String subjectCode, String examType) {
        try {
            // 计算偏移量
            Integer offset = (page - 1) * size;
            
            System.out.println("查询参数: userId=" + userId + ", page=" + page + ", size=" + size + ", subjectCode=" + subjectCode + ", examType=" + examType);
            
            // 获取分页数据
            List<WrongQuestionVO> data = wrongQuestionMapper.getWrongQuestionsByCount(userId, offset, size, subjectCode, examType);
            System.out.println("查询结果数量: " + (data != null ? data.size() : 0));
            
            // 获取总数
            Integer total = wrongQuestionMapper.getTotalCount(userId, subjectCode, examType);
            System.out.println("总数: " + total);
            
            // 组装返回结果
            Map<String, Object> result = new HashMap<>();
            result.put("data", data);
            result.put("total", total);
            result.put("page", page);
            result.put("size", size);
            
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public Map<String, Object> getBySubjectChapter(Long userId, Integer page, Integer size, String subjectCode, String examType) {
        // 计算偏移量
        Integer offset = (page - 1) * size;
        
        System.out.println("查询参数: userId=" + userId + ", page=" + page + ", size=" + size + ", subjectCode=" + subjectCode + ", examType=" + examType);
        
        // 获取分页数据
        List<WrongQuestionVO> data = wrongQuestionMapper.getWrongQuestionsBySubjectChapter(userId, offset, size, subjectCode, examType);
        System.out.println("查询结果数量: " + (data != null ? data.size() : 0));
        
        // 获取总数
        Integer total = wrongQuestionMapper.getTotalCount(userId, subjectCode, examType);
        System.out.println("总数: " + total);
        
        // 组装返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("data", data);
        result.put("total", total);
        result.put("page", page);
        result.put("size", size);
        
        return result;
    }

    @Override
    public Map<String, Object> getByTimeOrder(Long userId, boolean isAsc, Integer page, Integer size, String subjectCode, String examType) {
        String order = isAsc ? "ASC" : "DESC";
        
        // 计算偏移量
        Integer offset = (page - 1) * size;
        
        System.out.println("查询参数: userId=" + userId + ", page=" + page + ", size=" + size + ", subjectCode=" + subjectCode + ", examType=" + examType);
        
        // 获取分页数据
        List<WrongQuestionVO> data = wrongQuestionMapper.getWrongQuestionsByTime(userId, isAsc, offset, size, subjectCode, examType);
        System.out.println("查询结果数量: " + (data != null ? data.size() : 0));
        
        // 获取总数
        Integer total = wrongQuestionMapper.getTotalCount(userId, subjectCode, examType);
        System.out.println("总数: " + total);
        
        // 组装返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("data", data);
        result.put("total", total);
        result.put("page", page);
        result.put("size", size);
        
        return result;
    }

    @Override
    public int updateWrongCount(Long id, int newCount) {
        WrongQuestion entity = new WrongQuestion();
        entity.setId(id);
        entity.setWrongCount(newCount);
        return wrongQuestionMapper.updateWrongCountByEntity(entity);
    }

    @Override
    public int deleteWrongQuestion(Long id) {
        return wrongQuestionMapper.deleteWrongQuestion(id);
    }

    @Override
    public Map<String, Object> getWeakPointsByUserId(Long userId, Integer subjectId, Integer page, Integer size) {
        // 计算偏移量
        Integer offset = (page - 1) * size;
        
        // 获取分页数据
        List<Map<String, Object>> data = wrongQuestionMapper.getWeakPointsByUserId(userId, subjectId, offset, size);
        
        // 获取总数
        Integer total = wrongQuestionMapper.getWeakPointsCount(userId, subjectId);
        
        // 组装返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("data", data);
        result.put("total", total);
        result.put("page", page);
        result.put("size", size);
        
        return result;
    }

    @Override
    public boolean toggleFavorite(Long userId, Long questionId, boolean isFavorite) {
        try {
            if (isFavorite) {
                // 添加收藏
                return wrongQuestionMapper.addFavorite(userId, questionId) > 0;
            } else {
                // 取消收藏
                return wrongQuestionMapper.removeFavorite(userId, questionId) > 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
